package com.lm.dao.impl;

import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.lm.dao.BoardDAO;
import com.lm.model.Board;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.util.DBUtil;
import com.util.DataGeneration;

public class BoardDAOImpl implements BoardDAO, DataGeneration {
	private ComboPooledDataSource dataSource;
	private List<Board> resList = new ArrayList<Board>();

	public void setDataSource(ComboPooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	public List<Board> getAllBoard() throws Exception {
		return DBUtil.getCollectionOperation(dataSource,
				getSelectAllBoardSQL(), new BoardDAOImpl());
	}

	public Board getBoardByBoardID(String boardID) throws Exception {
		return (Board) DBUtil.getObjectOperation(dataSource,
				getSelectBoardByBoardID(Integer.parseInt(boardID)),
				new BoardDAOImpl());
	}

	public Board getBoardByBoardName(String boardName) throws Exception {
		return (Board) DBUtil.getObjectOperation(dataSource,
				getSelectBoardByBoardName(boardName), new BoardDAOImpl());
	}

	public void addBoard(Board board) throws Exception {
		DBUtil.addOperation(dataSource, getAddBoardSQL(board));
	}

	public void deleteBoardByBoardID(String boardID) throws Exception {
		DBUtil.editOrDeleteOperation(dataSource,
				getDeleteBoardByBoardIDSQL(boardID));
	}

	public void editBoardByBoardID(Board board, String boardID)
			throws Exception {
		DBUtil.editOrDeleteOperation(dataSource, getEditBoardByBoardIDSQL(
				board, boardID));
	}

	public void editBoardContentByBoardID(Board board, String boardID)
			throws Exception {
		DBUtil.editOrDeleteOperation(dataSource,
				getEditBoardContentByBoardIDSQL(board, boardID));
	}

	private String getDeleteBoardByBoardIDSQL(String boardID) {
		StringBuilder sb = new StringBuilder();
		sb.append(" delete from lm_board");
		sb.append(" where id=").append(Integer.parseInt(boardID));
		return sb.toString();
	}

	private String getSelectAllBoardSQL() {
		StringBuilder sb = new StringBuilder();

		sb.append(" select * from lm_board ");
		return sb.toString();
	}

	private String getSelectBoardByBoardID(int boardID) {
		StringBuilder sb = new StringBuilder();

		sb.append(" select * from lm_board ");
		sb.append(" where id=").append(boardID);
		return sb.toString();
	}

	private String getSelectBoardByBoardName(String boardName) {
		StringBuilder sb = new StringBuilder();

		sb.append(" select * from lm_board ");
		sb.append(" where boardName like '%").append(boardName).append("%'");
		return sb.toString();
	}

	private String getAddBoardSQL(Board board) {
		StringBuilder sb = new StringBuilder();
		sb.append(" insert into lm_board(boardName,boardImgURL,boardContent)");
		sb.append(" values('").append(board.getBoardName()).append("',");
		sb.append("'").append(board.getBoardImgUrl()).append("',");
		sb.append("'").append(board.getBoardContent()).append("')");
		return sb.toString();
	}

	private String BlobToString(Blob blob) throws SQLException {
		byte[] bdata = blob.getBytes(1, (int) blob.length());
		String text = new String(bdata);

		return text;
	}

	private String getEditBoardByBoardIDSQL(Board board, String boardID) {
		StringBuilder sb = new StringBuilder();

		sb.append("update lm_board set boardName='").append(
				board.getBoardName());
		sb.append("',boardImgURL='").append(board.getBoardImgUrl()).append("'");

		sb.append(" where id=").append(boardID);
		return sb.toString();
	}

	private String getEditBoardContentByBoardIDSQL(Board board, String boardID) {
		StringBuilder sb = new StringBuilder();

		sb.append("update lm_board set boardContent='").append(
				board.getBoardContent()).append("'");
		sb.append(" where id=").append(boardID);
		return sb.toString();
	}

	public List<Board> getResList(ResultSet rs) throws SQLException {
		while (rs.next()) {
			Board board = new Board();
			setBoard(rs, board);
			resList.add(board);
		}

		return resList;
	}

	private void setBoard(ResultSet rs, Board board) throws SQLException {
		board.setId(rs.getInt("id"));
		board.setBoardName(rs.getString("boardName"));
		board.setBoardImgUrl(rs.getString("boardImgURL"));
		board.setBoardContent(BlobToString(rs.getBlob("boardContent")));
	}

	public Board getResModel(ResultSet rs) throws SQLException {
		Board board = new Board();
		while (rs.next()) {
			setBoard(rs, board);
		}

		return board;
	}

}
